**************************************************************
* Author: Michael Tomz
* Date: Dec 20, 2001
* Format: Stata do-file
* Purpose: Replicate Section 5.3 (Percent Correctly Predicted)
**************************************************************

version 6.0
clear
set mem 100m

* PERCENT CORRECTLY PREDICTED UNDER KK
tempname pcpF pcpP pcpFc pcpPc
use ukout, clear
collapse (median) pcpfc1 pcpfc pcpfc2 pcppc1 pcppc pcppc2, by(year)
mkmat year pcpfc pcpfc1 pcpfc2, mat(`pcpF')                /* fully contested */
mkmat year pcppc pcppc1 pcppc2 if year <=74 , mat(`pcpP')  /* partially contested */
* PREPARE KK DATASETS FOR GRAPHING
preserve
svmat `pcpF', names(pcpF)
keep pcpF1 pcpF2 pcpF3 pcpF4
keep in 1/10
gen x = _n
save pcpF, replace old
restore
svmat `pcpP', names(pcpP)
keep pcpP1 pcpP2 pcpP3 pcpP4
keep in 1/5
gen x = _n
save pcpP, replace old


* PERCENT CORRECTLY PREDICTED UNDER SUR (takes a while to run)
matrix `pcpFc' = J(10,4,0)
matrix `pcpPc' = J(5,4,0)
local i 1
local years 59 64 66 70 74 74a 79 83 87 92
while "`years'" ~= "" {
   gettoken year years : years
   use r`year'v, clear      /* load full sampling distribution of E(vote) */
   gen truewin = cond(max(v1,v2,v3)==v1,1,cond(max(v1,v2,v3)==v2,2,3)) 
   gen ewin = cond(max(ev1c,ev2c,ev3c)==ev1c,1,cond(max(ev1c,ev2c,ev3c)==ev2c,2,3))
   gen correct = cond(ewin==truewin,1,0)
   egen pcpfc = mean(correct) if fully==1, by(simid)
   egen pcppc = mean(correct) if fully==0, by(simid)
   collapse (median) pcpfc pcppc, by(simid)
   su pcpfc, meanonly
   mat `pcpFc'[`i',2] = r(mean)
   _pctile pcpfc, p(2.5,97.5)
   mat `pcpFc'[`i',3] = r(r1)  /* lower bound of 95% ci */
   mat `pcpFc'[`i',4] = r(r2)  /* upper bound of 95% ci */
   if "`year'" == "74a" { local year 74.9 }
   mat `pcpFc'[`i',1] = `year'
   if `i' <= 5 {          /* partially contested */
      su pcppc, meanonly
      mat `pcpPc'[`i',2] = r(mean)
      _pctile pcppc, p(2.5,97.5)
      mat `pcpPc'[`i',3] = r(r1)  /* lower bound of 95% ci */
      mat `pcpPc'[`i',4] = r(r2)  /* upper bound of 95% ci */
      mat `pcpPc'[`i',1] = `year'
   }
   local i = `i' + 1
}
* PREPARE SUR DATASETS FOR GRAPHING
preserve
svmat `pcpFc', names(pcpFc)
keep pcpFc1 pcpFc2 pcpFc3 pcpFc4
keep in 1/10
gen x = _n
save pcpFc, replace old
restore
svmat `pcpPc', names(pcpPc)
keep pcpPc1 pcpPc2 pcpPc3 pcpPc4
keep in 1/5
gen x = _n
save pcpPc, replace old

